﻿
<link rel="stylesheet" type="text/css" href='@Url.Content("~/Scripts/tab/css/easyui.css")'/>
<link rel="stylesheet" type="text/css" href='@Url.Content("~/Scripts/tab/css/icon.css")'/>
<script type="text/javascript" src='@Url.Content("~/Scripts/tab/js/jquery.easyui.min.js")'></script>
 

@using (Html.BeginForm("UserPermissionSave", "Account", Html.ViewContext.RouteData.Values, FormMethod.Post))
{
    <div style="width:1240px ;text-align:left;margin-top:10px;margin-left:5px;margin-bottom:5px">
        <input type="submit" class="webcar-button" value="保存" onclick="GetCheckedNodes();"  />
        <input type="button" class="webcar-button" value="取消" onclick="pageback();" />
    </div>
    @Html.AntiForgeryToken()
    @Html.Hidden("selectPermissions", ((string)@ViewBag.SelectedRowIds), new { @style = "width:100%" })
@Html.Hidden("selectRevokePermissions", ((string)@ViewBag.RevokeSelectedRowIds), new { @style = "width:100%" })

    @Html.Hidden("selectGroupList", ((string)@ViewBag.SelectedGroupIds), new { @style = "width:100%" })
    @Html.Hidden("selectDept")
    
    <script type="text/javascript">
        var selectedIDs = '@ViewBag.SelectedRowIds'.split(";");
        var selectedRevokeIDs = '@ViewBag.RevokeSelectedRowIds'.split(";");

        var selectedGroups = '@ViewBag.SelectedGroupIds'.split(";");
        var select_nodes = "";
        function GetCheckedNodes() {
            select_nodes = "";
            var node = deptTree.GetRootNode();
            GetSubCheckedNodes(node);
            
            $("#selectDept").val(select_nodes);
        };
        function GetSubCheckedNodes(node) {
            
            for (var i = 0; i < node.GetNodeCount() ; i++) {
                var childNode = node.GetNode(i);
                if (childNode.GetCheckState() != "Unchecked") {
                    //select_nodes = select_nodes + childNode.GetText() + ";";
                    select_nodes = select_nodes + childNode.name + ";";

                    GetSubCheckedNodes(childNode);
                }
            }
        };

        function Permission_OnSelectionChanged(s, e) {
            if (e.visibleIndex < 0) return;
            
            var key = s.GetRowKey(e.visibleIndex);
            if (key == null) return;
            var index = selectedIDs.indexOf(key);
            if (e.isSelected && index < 0) {
                selectedIDs.push(key);
            }
            else {
                if (index >= 0)
                    selectedIDs.splice(index, 1);
            }
            $("#selectPermissions").val(selectedIDs.join(";"));
        };

        function Permission_OnSelectionChanged2(s, e) {
            if (e.visibleIndex < 0) return;

            var key = s.GetRowKey(e.visibleIndex);
            if (key == null) return;
            var index = selectedRevokeIDs.indexOf(key);
            if (e.isSelected && index < 0) {
                selectedRevokeIDs.push(key);
            }
            else {
                if (index >= 0)
                    selectedRevokeIDs.splice(index, 1);
            }
            $("#selectRevokePermissions").val(selectedRevokeIDs.join(";"));
        };

        function Group_OnSelectionChanged(s, e) {
            if (e.visibleIndex < 0) return;
            var key = s.GetRowKey(e.visibleIndex);
            if (key == null) return;
            var index = selectedGroups.indexOf(key);
            if (e.isSelected && index < 0) {
                selectedGroups.push(key);
            }
            else {
                if (index >= 0)
                    selectedGroups.splice(index, 1);
            }
            $("#selectGroupList").val(selectedGroups.join(";"));
        };
        function OnTreeViewCheckedChanged(s, e) {
        };
    </script>
    
    <div id="tt2" class="easyui-tabs" style="width:100%;height:auto">
        <div title="用户权限组" style="padding:20px;"  >
                    @Html.DevExpress().GridView(
                    settings =>
                    {
                        settings.Name = "gridView_user_group";
                        DevControlHelper.InitDefaultPagerGridViewSettings(settings);
                        settings.SettingsBehavior.AllowSort = false;
                        settings.Width = Unit.Pixel(1200);
                        settings.Height = Unit.Percentage(100);
                        settings.HtmlRowCreated += (s, e) =>
                        {
                            MVCxGridView grid = s as MVCxGridView;
                            if (e.VisibleIndex < 0) return;
                            var selected = grid.GetRowValues(e.VisibleIndex, "Selected");
                            if ((bool)selected)
                            {
                                grid.Selection.SetSelection(e.VisibleIndex, true);
                            }
                        };

                        settings.SettingsPager.Visible = false;
                        settings.SettingsPager.Mode = GridViewPagerMode.ShowAllRecords;
                        settings.CallbackRouteValues = new { Controller = "Account", Action = "UserPermissions", id = Html.ViewContext.RouteData.Values["id"] };

                        settings.KeyFieldName = "GROUP_CODE";
                        settings.Columns.Add("GROUP_NOTE", "权限组名");
                        settings.Columns.Add("NOTE", "备注");
                        settings.AddYesNoColumn("IS_DEFAULT", "是否默认组");
                        //settings.Columns.Add("IsDefault", "是否默认组").ColumnType = MVCxGridViewColumnType.CheckBox;
                        settings.CommandColumn.Visible = true;
                        settings.CommandColumn.ShowSelectCheckbox = true;
                        settings.ClientSideEvents.SelectionChanged = "Group_OnSelectionChanged";
                    }).Bind(ViewBag.GroupList).GetHtml()
            </div>
             <div title="已授予的权限" style="padding:20px;"  >       
                    @Html.DevExpress().GridView(
                    settings =>
                    {
                        settings.Name = "gridView_userp";
                        DevControlHelper.InitDefaultPagerGridViewSettings(settings);
                        settings.SettingsBehavior.AllowSort = false;
                        settings.Width = Unit.Pixel(1200);
                        settings.Height = Unit.Percentage(100);
                        settings.HtmlRowCreated += (s, e) =>
                        {
                            MVCxGridView grid = s as MVCxGridView;
                            if (e.VisibleIndex < 0) return;
                            var selected = grid.GetRowValues(e.VisibleIndex, "Selected");
                            string permission_type = grid.GetRowValues(e.VisibleIndex, "PurviewType").ToSafeString();
                            if ((bool)selected && permission_type != "revoke")
                            {
                                grid.Selection.SetSelection(e.VisibleIndex, true);
                            }
                        };

                        settings.SettingsPager.Visible = false;
                        settings.SettingsPager.Mode = GridViewPagerMode.ShowAllRecords;
                        settings.CallbackRouteValues = new { Controller = "Account", Action = "UserPermissions", id = Html.ViewContext.RouteData.Values["id"] };

                        settings.KeyFieldName = "PermissionId";
                        settings.Columns.Add("PermissionType", "分类");
                        settings.Columns.Add("Source", "来源").CellStyle.HorizontalAlign = HorizontalAlign.Left;
                        settings.Columns.Add("Name", "权限名称");
                        settings.Columns.Add("Module", "模块");
                        settings.CommandColumn.Visible = true;
                        settings.CommandColumn.ShowSelectCheckbox = true;
                        settings.ClientSideEvents.SelectionChanged = "Permission_OnSelectionChanged";
                        settings.CommandButtonInitialize += (s, e) =>
                        {
                            if (e.VisibleIndex == -1) return;
                            if (e.ButtonType == ColumnCommandButtonType.SelectCheckbox)
                            {
                                MVCxGridView grid = s as MVCxGridView;
                                string permission_source = grid.GetRowValues(e.VisibleIndex, "Source").ToSafeString();
                                if (permission_source.In("2", "3"))
                                {
                                    e.Enabled = false;
                                }
                            }
                        };
                        settings.CustomColumnDisplayText += (s, e) =>
                        {
                            if (e.Column.FieldName == "Source")
                            {
                                if (e.Value.ToSafeString() == "2")
                                {
                                    e.DisplayText = "权限组";
                                }
                                else if (e.Value.ToSafeString() == "1")
                                {
                                    e.DisplayText = "手动赋权";
                                }
                                else if (e.Value.ToSafeString() == "4")
                                {
                                    e.DisplayText = "权限已拒绝";
                                }
                                    
                                else
                                {
                                    e.DisplayText = "默认权限";
                                }
                            }

                        };
                    }).Bind(Model).GetHtml()
                 </div>
                <div title="拒绝授予的权限" style="padding:20px;"  >
                    @Html.DevExpress().GridView(
                    settings =>
                    {
                        settings.Name = "gridView_userp_revoke";
                        DevControlHelper.InitDefaultPagerGridViewSettings(settings);
                        settings.SettingsBehavior.AllowSort = false;
                        settings.Width = Unit.Pixel(1200);
                        settings.Height = Unit.Percentage(100);
                        settings.HtmlRowCreated += (s, e) =>
                        {
                            MVCxGridView grid = s as MVCxGridView;
                            if (e.VisibleIndex < 0) return;
                            var selected = grid.GetRowValues(e.VisibleIndex, "Selected");
                            if ((bool)selected)
                            {
                                grid.Selection.SetSelection(e.VisibleIndex, true);
                            }
                        };

                        settings.SettingsPager.Visible = false;
                        settings.SettingsPager.Mode = GridViewPagerMode.ShowAllRecords;
                        settings.CallbackRouteValues = new { Controller = "Account", Action = "UserPermissions", id = Html.ViewContext.RouteData.Values["id"] };

                        settings.KeyFieldName = "PermissionId";
                        settings.Columns.Add("PermissionType", "分类");
                        //settings.Columns.Add("Source", "来源").CellStyle.HorizontalAlign = HorizontalAlign.Left;
                        settings.Columns.Add("Name", "权限名称");
                        settings.Columns.Add("Module", "模块");
                        settings.CommandColumn.Visible = true;
                        settings.CommandColumn.ShowSelectCheckbox = true;
                        settings.ClientSideEvents.SelectionChanged = "Permission_OnSelectionChanged2";
                    }).Bind(ViewBag.RevokeList).GetHtml()
                </div>
        <div title="可操作单位" style="padding:20px;">
            
    @Html.DevExpress().TreeView(settings => {
    string userid = Html.ViewContext.RouteData.Values["id"].ToString();
            var dept_query = (from p in WebcarDataProvider.DB.sys_dept
                             join p2 in WebcarDataProvider.DB.BS_MAN_DEPT.Where(a => a.MAN_CODE == userid)
            on p.deptcode equals p2.DEPT_CODE
            
            into pp
            from bb in pp.DefaultIfEmpty()
            where p.use_flag=="T"
            select new
            {
                DEPT_CODE = p.deptcode,
                DEPT_NAME = p.deptname,
                SUPER_DEPT = p.super_dept,
                IsSelected = bb == null ? false : true
            }).ToList();
    
        settings.Name = "deptTree";
        settings.EnableAnimation = true;
        settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);
        foreach (var dept in dept_query.Where(a => a.SUPER_DEPT.IsNull()))
        {
            MVCxTreeViewNode dept_node = new MVCxTreeViewNode();
            dept_node.Text = dept.DEPT_NAME;
            dept_node.Name = dept.DEPT_CODE;
            dept_node.Image.Url = "~/Content/Images/Leaf.gif";
            //dept_node.NavigateUrl = Url.Action(Model.Action, Model.Controller, new { id = dept.DEPTCODE });
            dept_node.Checked = dept.IsSelected;
            //第二级部门
            foreach (var child_dept in dept_query.Where(a => a.SUPER_DEPT == dept.DEPT_CODE))
            {
                MVCxTreeViewNode child_dept_node = new MVCxTreeViewNode();
                child_dept_node.Text = child_dept.DEPT_NAME ;
                child_dept_node.Name = child_dept.DEPT_CODE;
                child_dept_node.Image.Url = "~/Content/Images/Leaf.gif";
                //child_dept_node.NavigateUrl = Url.Action(Model.Action, Model.Controller, new { id = child_dept.DEPT_CODE });
                child_dept_node.Checked = child_dept.IsSelected;
                
                dept_node.Nodes.Add(child_dept_node);
            }
            settings.Nodes.Add(dept_node);
        }
        settings.EnableHotTrack = true;
        settings.ShowTreeLines = true;
        settings.ShowExpandButtons = true;
        settings.AllowSelectNode = true;
        settings.AllowCheckNodes = true;
        settings.CheckNodesRecursive = true;
        settings.ClientSideEvents.CheckedChanged = "OnTreeViewCheckedChanged";
        settings.PreRender = (source, e) =>
        {
            //ASPxTreeView treeView = (ASPxTreeView)source;
            //treeView.ExpandToDepth(0);
            
            //TreeViewNode node = treeView.Nodes.FindByName(id);
            //treeView.SelectedNode = node;
            //if (node != null)
            //{
            //    treeView.ExpandToNode(node);
            //}
            
        };
    }).GetHtml()

        </div>
    </div>
    
    
    <script type="text/javascript">
        $(function () {
            $('#tt2').tabs({
                width: $("#tt2").parent().width(),
                border: false,
                plain: true
                //height: "auto"//$("#tt2").parent().height()
            });
        });
        function pageback() {
            location.href = '@Url.Action("List")';
        };
        
    </script>
}


